package com.healthcloud.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.healthcloud.dao.FamilyHistoryMapper;
import com.healthcloud.entity.FamilyHistory;
import com.healthcloud.service.FamilyHistoryService;

/**
 * Created by XYC on 2015/7/14.
 */
@Service
public class FamilyHistoryServiceImpl implements FamilyHistoryService {
    @Autowired
    private FamilyHistoryMapper familyHistoryMapper;

    /**
     * 家庭病史添加
     * @param familyHistories
     * @return
     */
    @Override
    public FamilyHistory save(FamilyHistory familyHistories) {
//        familyHistoryMapper.deleteWithIdCard(familyHistories.get(0).getIdCard());
        List<FamilyHistory> old = familyHistoryMapper.findByEntityWithoutPage(familyHistories);
        if (old.isEmpty())
        {
            familyHistories.setCreatedTime(new Date());
            familyHistories.setUpdatedTime(new Date());
            familyHistoryMapper.save(familyHistories);
        }
        return familyHistories;
    }

    @Override
    public Boolean deletOneData(FamilyHistory familyHistories) {
        List<FamilyHistory> old = familyHistoryMapper.findByEntityWithoutPage(familyHistories);
        if (old.isEmpty()){
            return false;
        }
        familyHistoryMapper.deleteWithId(old.get(0).getId());
        return true;
    }

    @Override
    public FamilyHistory saveOrUpData(FamilyHistory familyHistory) {
        FamilyHistory Temp=new FamilyHistory();
        Temp.setIdCard(familyHistory.getIdCard());
        Temp.setFamilyRelatives(familyHistory.getFamilyRelatives());
        List<FamilyHistory> old = familyHistoryMapper.findByEntityWithoutPage(Temp);
        if (old.isEmpty())
        {
            familyHistory.setCreatedTime(new Date());
            familyHistory.setUpdatedTime(new Date());
            familyHistoryMapper.save(familyHistory);
        }else
        {
            familyHistory.setId(old.get(0).getId());
            familyHistory.setUpdatedTime(new Date());
            familyHistoryMapper.update(familyHistory);
        }
        return familyHistory;
    }

    @Override
    public ArrayList<FamilyHistory> findByIdCard(FamilyHistory familyHistorie) {
//        FamilyHistory familyHistory = new FamilyHistory();
//        familyHistory.setIdCard(idCard);
        return (ArrayList<FamilyHistory>) familyHistoryMapper.findByEntityWithoutPage(familyHistorie);
//        return null;
    }
}
